package lecture;

import beans.SessionManagedBean;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import lecture.Lecture;
import lecture.LectureAdminManagedBean;
import user.Role;
import user.User;
import user.UserAdminManagedBean;

/**
 *
 * @author Thorsten Ludwig
 */
@ManagedBean(name="newLectureBean")
@RequestScoped
public class NewLectureManagedBean {
    
    private String lectureName;
    private String lectureId;

    public NewLectureManagedBean() {
    }
    
    public String getLectureId() {
        return lectureId;
    }

    public void setLectureId(String lectureId) {
        this.lectureId = lectureId;
    }

    public String getLectureName() {
        return lectureName;
    }

    public void setLectureName(String lectureName) {
        this.lectureName = lectureName;
    }
    
    public String addLecture() {
        FacesContext c = FacesContext.getCurrentInstance();
        LectureAdminManagedBean l = c.getApplication().evaluateExpressionGet(c, "#{lectureAdmin}", LectureAdminManagedBean.class);
        UserAdminManagedBean ub = c.getApplication().evaluateExpressionGet(c, "#{userAdminBean}", UserAdminManagedBean.class);
        Lecture lecture = new Lecture(this.lectureName);
        lecture.getInfo().setLectureId(this.lectureId);
        SessionManagedBean sb = c.getApplication().evaluateExpressionGet(c, "#{sessionBean}", SessionManagedBean.class);
        for (User u: ub.getUsers()) {
            u.assignRole(this.lectureName, Role.GUEST);
        }
        User u = sb.getUser();        
        l.addLecture(lecture);
        lecture.addUser(u);
        u.assignRole(lecture.getInfo().getName(), Role.ADMIN);
        sb.init();
        
        return "loggedIn.jsf";
    }
}
